{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fe5c6720-1a5a-43ac-9e02-d2fd9fac7f63",
   "metadata": {},
   "source": [
    "# Pandas\n",
    "\n",
    "## 1.Pandas数据结构\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7d860955-0774-4c73-a9fe-e99c4269aabe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad5b32ac-2512-4f52-995e-70a0a68b58ff",
   "metadata": {},
   "source": [
    "### 1.1 Series\n",
    "是一种类似于一维数组的数据结构,由两个部分组成\n",
    "- values:一组数据（ndarray类型）\n",
    "- index:相关的数据索引标签"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0c80fed-b2a7-47d7-91cb-0be19c10f066",
   "metadata": {},
   "source": [
    "1) Serires的创建\n",
    "\n",
    "方式一：由列表或NumPy数组创建\n",
    "默认索引为0到N-1的整数型索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "303f3cbf-d459-4405-b4cd-3996288c9211",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    11\n",
       "1    22\n",
       "2    33\n",
       "3    44\n",
       "dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1=[11,22,33,44]\n",
    "\n",
    "s=pd.Series(list1)\n",
    "s\n",
    "\n",
    "n=np.array(list1)\n",
    "s=pd.Series(list1)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d372af58-92dc-4b46-9ad5-9d83bf49a38c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d09779ac-e651-4781-bbaa-48ebe9fc2028",
   "metadata": {},
   "source": [
    "- index和values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4bc1c502-1c19-4e7c-bc4a-55764f9b6cb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 22, 33, 44])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.values\n",
    "#值，为一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c19bbaaa-2930-4f44-8907-8485859108cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=4, step=1)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index\n",
    "# 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1bb54338-48ee-430a-a259-bc4e940af4ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    11\n",
       "b    22\n",
       "c    33\n",
       "d    44\n",
       "dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#修改索引\n",
    "s.index=['a','b','c','d']\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "550d8396-2ffd-4eb6-9f93-7ba209f319f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(10)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#通过索引获取或修改值\n",
    "s.a\n",
    "s.a=10\n",
    "s.a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e8d8abd-19da-49ef-af34-e8fb73322ef2",
   "metadata": {},
   "source": [
    "2. 用字典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c1561066-dba1-4d2b-b006-7eb39d3d4f40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    [[1, 9, 2], [5, 0, 6]]\n",
       "b    [[5, 8, 3], [3, 6, 0]]\n",
       "c    [[5, 9, 1], [7, 1, 2]]\n",
       "d    [[4, 8, 0], [0, 9, 4]]\n",
       "dtype: object"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d={'a':11,'b':22,'c':33,'d':44}\n",
    "\n",
    "s=pd.Series(d)\n",
    "s\n",
    "\n",
    "d={'a':np.random.randint(0,10,size=(2,3)),\n",
    "   'b':np.random.randint(0,10,size=(2,3)),\n",
    "   'c':np.random.randint(0,10,size=(2,3)),\n",
    "   'd':np.random.randint(0,10,size=(2,3))}\n",
    "\n",
    "s=pd.Series(d)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "42a50d82-2de5-4268-a771-e00265df009f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "Name: 测试, dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series([1,2,3],index=['a','b','c'],name='测试')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "298aef60-8bc7-4266-aa0b-3a747e2b1039",
   "metadata": {},
   "source": [
    "2. Series的索引\n",
    "可以使用单括号取单个索引(此时返回的是元素类型)，或者中括号里一个列表取多个索引(此时返回的仍是一个Series类型)。分为显式索引，隐式索引\n",
    "(1) 显式索引：\n",
    "- 使用index中的元素作为索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "35d3c4d9-c2b3-4c14-8799-3620b05c2ce0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python    120\n",
       "NumPy     100\n",
       "Pandas    130\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series({'Python':120,'NumPy':100,'Pandas':130})\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d644fcd8-40a4-44e9-9225-c413c2272aa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python    120\n",
       "NumPy     100\n",
       "dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显式索引:使用索引名\n",
    "# 一次取单个值返回的值是元素类型\n",
    "s['Python']\n",
    "s.Python\n",
    "\n",
    "s[['Python','NumPy']]#一次取多,得到的类型是Series\n",
    "\n",
    "#使用loc\n",
    "s.loc['Python']\n",
    "s.loc[['Python','NumPy']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d36f3d8-c937-4a82-977a-cf3e1d52cd54",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_11512\\2016567208.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[0]\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_11512\\2016567208.py:4: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[[0,2]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Python    120\n",
       "Pandas    130\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 隐式索引\n",
    "# 使用数字下标\n",
    "s[0]\n",
    "s[[0,2]]\n",
    "\n",
    "# 使用iloc\n",
    "s.iloc[0]\n",
    "s.iloc[[0,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ca8992-cba8-4041-a1e4-01bb8b117895",
   "metadata": {},
   "source": [
    "3. Series的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a69e5d9a-7d0b-41ca-bb62-f5b4a22bccfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文        100\n",
       "数学        150\n",
       "英语        120\n",
       "python    130\n",
       "pandas    110\n",
       "numpy     150\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series({\n",
    "    '语文':100,\n",
    "    '数学':150,\n",
    "    '英语':120,\n",
    "    'python':130,\n",
    "    'pandas':110,\n",
    "    'numpy':150})\n",
    "\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e18480d2-214b-413a-a990-818597c6118f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "数学        150\n",
       "英语        120\n",
       "python    130\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "# 隐式切片：使用数字下标\n",
    "s[1:4] #左闭右开\n",
    "s.iloc[1:4]\n",
    "\n",
    "# 显式切片\n",
    "s['数学':'python'] #左闭右闭\n",
    "s.loc['数学':'python']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79e822da-ca97-4506-bd3c-23a1e18639e4",
   "metadata": {},
   "source": [
    "4. 基本属性与常用方法\n",
    "- shape 形状\n",
    "- size 长度\n",
    "- index 索引\n",
    "- values 值\n",
    "- name 名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ad1cfc73-922e-4c5c-963b-d0add6769372",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文        100\n",
       "数学        150\n",
       "英语        120\n",
       "python    130\n",
       "pandas    110\n",
       "numpy     150\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "be51eed9-77b8-4018-8895-e7b4780cc3ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "s.shape #形状(6,)\n",
    "s.size #长度(元素个数) 6\n",
    "s.index #索引\n",
    "s.values #值\n",
    "s.name #索引名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "999b063b-a924-40a4-8e6d-cb66e3fc0c34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "数学        150\n",
       "英语        120\n",
       "python    130\n",
       "pandas    110\n",
       "numpy     150\n",
       "dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# head(n)查看前n条数据默认5\n",
    "s.head()\n",
    "\n",
    "# tail(n)查看后n条数据默认5\n",
    "s.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41878ee9-8b59-4d4f-9d36-236e5b9ebf0a",
   "metadata": {},
   "source": [
    "检测缺失数据\n",
    "- pd.isnull()\n",
    "- pd.notnull()\n",
    "- isnull()\n",
    "- notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "231d709c-d58b-49c0-bb4b-f5ac080eeb71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      a\n",
       "1      b\n",
       "2      c\n",
       "3    NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s =pd.Series(['a','b','c',np.nan])\n",
    "s\n",
    "# NaN表示空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cb3fc780-27cd-4db6-8e8e-16cc0e6d62d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     True\n",
       "1     True\n",
       "2     True\n",
       "3    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.isnull(s)\n",
    "s.isnull()\n",
    "\n",
    "pd.notnull(s)\n",
    "s.notnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "127191c9-deb4-4d6e-814d-b8bb92a915ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "dtype: object"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用bool值索引过滤数据\n",
    "# 过滤空值\n",
    "\n",
    "cond1=s.isnull()\n",
    "cond1\n",
    "\n",
    "# s[[True,False,False,True]]\n",
    "s[~cond1]# ~ ：取反"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df0e24cb-cc82-4329-a109-93c33ef6ad50",
   "metadata": {},
   "source": [
    "5. Series的运算\n",
    "\n",
    "（1）适用于NumPy的数组运算也适用于Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "70dfd569-5a39-41f5-bd2c-cbfa667f0b57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    91\n",
       "1    82\n",
       "2    39\n",
       "3    71\n",
       "4    29\n",
       "5    82\n",
       "6    10\n",
       "7    75\n",
       "8    16\n",
       "9    29\n",
       "dtype: int32"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randint(10,100,size=10))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c07e8740-7906-40bf-8219-c755e83292ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8281\n",
       "1    6724\n",
       "2    1521\n",
       "3    5041\n",
       "4     841\n",
       "5    6724\n",
       "6     100\n",
       "7    5625\n",
       "8     256\n",
       "9     841\n",
       "dtype: int32"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 基本算术运算\n",
    "s+100\n",
    "s-100\n",
    "s*100\n",
    "s/100\n",
    "s//2\n",
    "s%2\n",
    "s**2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "253047e6-7dc8-416c-b7d8-82c2ef73edc9",
   "metadata": {},
   "source": [
    "(2)两个Series之间的运算\n",
    "- 在运算中自动对齐索引\n",
    "- 如果索引不对应，则用NaN补充\n",
    "- Series没有广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1db4842a-3c7a-4026-b759-8dadeed82bec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    15\n",
       "1    43\n",
       "2    19\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0    42\n",
       "1    54\n",
       "2    15\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "s1=pd.Series(np.random.randint(10,100,size=3))\n",
    "s2=pd.Series(np.random.randint(10,100,size=3))\n",
    "display(s1,s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d7013f1e-ec66-4aeb-ac1a-8a140fb0d61b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   -27\n",
       "1   -11\n",
       "2     4\n",
       "dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1+s2\n",
    "s1-s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b8cc400c-1af5-4a30-a926-f9332b2a2263",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    52\n",
       "1    23\n",
       "2    34\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0    18\n",
       "1    52\n",
       "2    52\n",
       "3    68\n",
       "dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "s3=pd.Series(np.random.randint(10,100,size=3))\n",
    "s4=pd.Series(np.random.randint(10,100,size=4))\n",
    "display(s3,s4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dd7698b3-1d67-4dc7-8df9-f653d853ff5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    33.0\n",
       "1    95.0\n",
       "2    71.0\n",
       "3     NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1+s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "25edb6c4-74e7-43bd-96ec-95416721ab79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3    18\n",
       "1    52\n",
       "2    52\n",
       "0    68\n",
       "dtype: int32"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s4.index=[3,1,2,0]\n",
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a72aa960-d038-40e1-9493-c1cb682434ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    120.0\n",
       "1     75.0\n",
       "2     86.0\n",
       "3      NaN\n",
       "dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对应索引的值进行运算\n",
    "s3+s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3ba15ab3-03e8-4201-901a-05239573f990",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    120.0\n",
       "1     75.0\n",
       "2     86.0\n",
       "3     18.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将缺失值指定一个默认值进行运算\n",
    "s3.add(s4,fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c56db7a-280c-4f7e-96a1-e9ef79abb473",
   "metadata": {},
   "source": [
    "总结：\n",
    "- Series：可以看作一个有序的字典结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a1d1697-3135-4344-a773-726797d0cde9",
   "metadata": {},
   "source": [
    "## DataFrame\n",
    "DataFrame是一个【表格型】的数据结构，可以看作是由Series组成的字典(共用同一个索引)。DataFrame由按一定顺序排列的多列数据组成。设计初衷是将Series的使用场景从一维拓展到多维。DataFrame既有行索引，也有列索引。\n",
    "- 行索引：index\n",
    "- 列索引：columns\n",
    "- 值：values(NumPy)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "684ae596-a270-425e-8120-def3714f7932",
   "metadata": {},
   "source": [
    "1）DataFrame的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c296de9a-2ae1-4bc0-9439-f4ed4424ba8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>number</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  number\n",
       "0    a      11\n",
       "1    b      22\n",
       "2    c      33\n",
       "3    d      44"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d={\n",
    "    'name':['a','b','c','d'],\n",
    "    'number':[11,22,33,44]\n",
    "}\n",
    "\n",
    "df=pd.DataFrame(d)\n",
    "df\n",
    "\n",
    "# 每一行被称为：一个样本(一条数据)\n",
    "# 每一列被称为：一种属性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99278e13-4627-4130-9c90-a51a4d2fd5c7",
   "metadata": {},
   "source": [
    "DataFrame的基本属性和方法\n",
    "- values值，二维ndarray数组\n",
    "- columns 列索引\n",
    "- index 行索引\n",
    "- shape 形状\n",
    "- head() 查看前几条数据，默认5\n",
    "- tail() 查看后几条数据，默认5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8c4ce7f9-4034-481c-82ff-91f4079983e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>number</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>d</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  number\n",
       "0    a      11\n",
       "1    b      22\n",
       "2    c      33\n",
       "3    d      44"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<bound method NDFrame.tail of   name  number\n",
       "0    a      11\n",
       "1    b      22\n",
       "2    c      33\n",
       "3    d      44>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display(df)\n",
    "\n",
    "#值\n",
    "df.values\n",
    "\n",
    "#列索引\n",
    "df.columns\n",
    "\n",
    "#行索引\n",
    "df.index\n",
    "\n",
    "#形状\n",
    "df.shape\n",
    "\n",
    "#查看前和后几条数据\n",
    "df.head\n",
    "df.tail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "9cc4f89b-1446-4123-b84a-53a3fe3f908a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name1</th>\n",
       "      <th>num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>a</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>b</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>c</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>d</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name1  num\n",
       "A     a   11\n",
       "B     b   22\n",
       "C     c   33\n",
       "D     d   44"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设置行索引\n",
    "df.index=list('ABCD')\n",
    "df\n",
    "\n",
    "# 设置列索引\n",
    "df.columns=['name1','num']\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "963f5049-1eac-404b-98c8-4eaf2ff8cc54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>number</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>a</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>b</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>c</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>d</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  number\n",
       "A    a      11\n",
       "B    b      22\n",
       "C    c      33\n",
       "D    d      44"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d={\n",
    "    'name':['a','b','c','d'],\n",
    "    'number':[11,22,33,44]\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(d,index=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7bfce5ab-6d5a-4b07-b854-c24adfd66e25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "      <td>17</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "张三  54  36  46  80  17  82\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame其他创建方式\n",
    "df=pd.DataFrame(\n",
    "    data=np.random.randint(10,100,size=(4,6)),\n",
    "    index=['张三','李四','王五','赵六'],\n",
    "    columns=['语文','数学','英语','化学','物理','生物']\n",
    ")\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adc883b8-81dd-45b8-bb28-ca927e37a92d",
   "metadata": {},
   "source": [
    "（2）DataFrame的索引\n",
    " 1 对列进行索引\n",
    "- 通过类似字典的方式\n",
    "- 通过属性的方式\n",
    "可以将DataFrame的列获取为一个Series。返回的Series拥有原DataFrame相同的索引，且name属性也已经设置好了，就是相应的列名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "15efc768-cf35-4569-b7ae-f158a872cb9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "      <td>17</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "张三  54  36  46  80  17  82\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4e5d0b81-61cd-4fdd-bbcf-b2e43d5c8b90",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Series\n",
    "df.语文\n",
    "s=df['语文']\n",
    "\n",
    "# 使用两个中括号得到的类型是：DataFrame\n",
    "df[['语文','化学']]\n",
    "d=df[['语文']]\n",
    "display(type(s),type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "934f4b5e-a980-4e36-ad3c-768e00f50312",
   "metadata": {},
   "source": [
    "（2）对行进行索引\n",
    "- 使用.loc[]加index来进行行索引\n",
    "- 使用.iloc[]加整数来进行行索引\n",
    "同样返回一个Series,index为原来的columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d6bd8a56-9ef7-4593-a075-a4f74f89518e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "      <td>17</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "张三  54  36  46  80  17  82\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "f3a6e178-43bf-441f-968b-a0f4ad51dbd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "pandas.core.frame.DataFrame"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 不可以直接取行索引\n",
    "# df['张三'] 报错\n",
    "# df.张三 报错\n",
    "\n",
    "# DataFrame默认先取列索引\n",
    "# 取行索引\n",
    "s=df.loc['张三']\n",
    "df.iloc[0]\n",
    "\n",
    "# 两个中括号返回DataFrame类型\n",
    "d=df.loc[['张三']]\n",
    "display(type(s),type(d))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f8c6b71-7162-44bd-83ab-18cdd7b28fa7",
   "metadata": {},
   "source": [
    "（3）对元素索引的方法\n",
    "- 使用列索引\n",
    "- 使用行索引(iloc[3,1]相当于两个参数)\n",
    "- 使用values属性(二维NumPy数组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "7659c8d9-abdf-476b-8981-16456de5a71d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "      <td>17</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "张三  54  36  46  80  17  82\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "7fe8c272-1c5c-43fc-b425-c1847db0ca4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_25960\\1855836438.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  df['语文'][0]\n",
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_25960\\1855836438.py:8: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  df.loc['张三'][0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int32(54)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先取列再取行\n",
    "df['语文']['张三']\n",
    "df['语文'][0]\n",
    "df.语文.张三\n",
    "\n",
    "# 先取行再取列\n",
    "df.loc['张三']['语文']\n",
    "df.loc['张三'][0]\n",
    "df.iloc[0]['语文']\n",
    "df.iloc[0,0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd0d1c0f-6a07-4d1e-8176-7a872fa74647",
   "metadata": {},
   "source": [
    "2)DataFrame的切片\n",
    "注意：直接使用中括号时：\n",
    "- 索引优先对列进行操作\n",
    "- 切片优先对行进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "61f1f7e4-046e-445a-b7da-316a172a0630",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>54</td>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "      <td>17</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "张三  54  36  46  80  17  82\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "26fda297-6347-4bcc-a65d-4f531201227e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "      <th>物理</th>\n",
       "      <th>生物</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>18</td>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "      <td>54</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "      <td>84</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>61</td>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "      <td>40</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  化学  物理  生物\n",
       "李四  18  35  35  26  54  81\n",
       "王五  48  82  67  46  84  11\n",
       "赵六  61  78  87  13  40  40"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行切片\n",
    "df[1:3] # 左闭右开\n",
    "df['李四':'赵六']# 左闭右闭\n",
    "\n",
    "df.iloc[1:3] # 左闭右开\n",
    "df.loc['李四':'赵六'] #左闭右闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "68129aae-3006-40c9-a1bb-e92028cd45a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>化学</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>36</td>\n",
       "      <td>46</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>35</td>\n",
       "      <td>35</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>82</td>\n",
       "      <td>67</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>78</td>\n",
       "      <td>87</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    数学  英语  化学\n",
       "张三  36  46  80\n",
       "李四  35  35  26\n",
       "王五  82  67  46\n",
       "赵六  78  87  13"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列切片\n",
    "# 对列做切片必须先对行做切片\n",
    "df.iloc[:,1:4] # 左闭右开\n",
    "df.loc[:,'数学':'化学']# 左闭右闭"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2b64df6-f8ec-4424-88b1-5e82c6ab8bd4",
   "metadata": {},
   "source": [
    "3）DataFrame的运算\n",
    "\n",
    "(1)DataFrame之间的运算\n",
    "- 在运算中自动对齐不同索引值的数据\n",
    "- 如果索引不对应则用NaN补充\n",
    "- DataFrame没有广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "98d05948-5bc2-4641-96b2-a8f0e8a349ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>94</td>\n",
       "      <td>20</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>25</td>\n",
       "      <td>57</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>10</td>\n",
       "      <td>38</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  94  20  46\n",
       "李四  25  57  33\n",
       "王五  10  38  99"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>65</td>\n",
       "      <td>97</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>35</td>\n",
       "      <td>54</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>45</td>\n",
       "      <td>40</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  65  97  89\n",
       "李四  35  54  98\n",
       "王五  45  40  54"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>98</td>\n",
       "      <td>70</td>\n",
       "      <td>41</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>72</td>\n",
       "      <td>18</td>\n",
       "      <td>31</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>48</td>\n",
       "      <td>19</td>\n",
       "      <td>77</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>25</td>\n",
       "      <td>62</td>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语  物理\n",
       "张三  98  70  41  34\n",
       "李四  72  18  31  32\n",
       "王五  48  19  77  68\n",
       "赵六  25  62  20  30"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "df1 =df=pd.DataFrame(\n",
    "    data=np.random.randint(10,100,size=(3,3)),\n",
    "    index=['张三','李四','王五'],\n",
    "    columns=['语文','数学','英语']\n",
    ")\n",
    "\n",
    "df2 =df=pd.DataFrame(\n",
    "    data=np.random.randint(10,100,size=(3,3)),\n",
    "    index=['张三','李四','王五'],\n",
    "    columns=['语文','数学','英语']\n",
    ")\n",
    "\n",
    "df3 =df=pd.DataFrame(\n",
    "    data=np.random.randint(10,100,size=(4,4)),\n",
    "    index=['张三','李四','王五','赵六'],\n",
    "    columns=['语文','数学','英语','物理']\n",
    ")\n",
    "\n",
    "display(df1,df2,df3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "88efab9c-ec5b-47ba-97dd-373055c37953",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三   1   1   0\n",
       "李四   1   1   1\n",
       "王五   1   1   1"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame和标量之间的运算\n",
    "df1 + 100\n",
    "df1 - 100\n",
    "df1 * 100\n",
    "df1 / 100\n",
    "df1 // 2\n",
    "df1 ** 2\n",
    "df1 % 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "3bfd6720-50a3-4037-8d51-443e44d0cd4a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>159</td>\n",
       "      <td>117</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>60</td>\n",
       "      <td>111</td>\n",
       "      <td>131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>55</td>\n",
       "      <td>78</td>\n",
       "      <td>153</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     语文   数学   英语\n",
       "张三  159  117  135\n",
       "李四   60  111  131\n",
       "王五   55   78  153"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#DataFrme之间的运算\n",
    "df1 + df2 #对应索引数的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "d5610d68-13bf-4b82-b1ee-eaca53e748f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>物理</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>90.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>87.0</td>\n",
       "      <td>192.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>75.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>64.0</td>\n",
       "      <td>97.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>57.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>176.0</td>\n",
       "      <td>58.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      数学  物理     英语     语文\n",
       "张三  90.0 NaN   87.0  192.0\n",
       "李四  75.0 NaN   64.0   97.0\n",
       "王五  57.0 NaN  176.0   58.0\n",
       "赵六   NaN NaN    NaN    NaN"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 + df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "5928f52a-31ff-4a33-b7f3-49f77fa94815",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>物理</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>90.0</td>\n",
       "      <td>34.0</td>\n",
       "      <td>87.0</td>\n",
       "      <td>192.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>75.0</td>\n",
       "      <td>32.0</td>\n",
       "      <td>64.0</td>\n",
       "      <td>97.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>57.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>58.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>62.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>25.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      数学    物理     英语     语文\n",
       "张三  90.0  34.0   87.0  192.0\n",
       "李四  75.0  32.0   64.0   97.0\n",
       "王五  57.0  68.0  176.0   58.0\n",
       "赵六  62.0  30.0   20.0   25.0"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用.add()方式填充数据\n",
    "df1.add(df3,fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23f2ea2b-6823-4029-b05f-525409958616",
   "metadata": {},
   "source": [
    "（2）DataFrme和Series之间的运算\n",
    "- 使用Python操作符：以行为单位操作(参数必须是行),对所有行都有效\n",
    "- - 类似NumPy中的二维数组与一维数组的运算，但可能会出现NaN\n",
    " \n",
    "- 使用Pandas操作函数：\n",
    "- - axis=0：以列为单位操作(参数必须是列)，对所有行都有效\n",
    "  - axis=1：以行为单位操作(参数必须是行),对所有行都有效"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "e854e86c-adb8-412b-bdf0-191e36f46ef5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>94</td>\n",
       "      <td>20</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>25</td>\n",
       "      <td>57</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>10</td>\n",
       "      <td>38</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  94  20  46\n",
       "李四  25  57  33\n",
       "王五  10  38  99"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "92f78600-3b9b-48d9-a319-153fc0abe59b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    100\n",
       "数学     10\n",
       "英语      1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series([100,10,1],index=df1.columns)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "b42d2701-30f8-4f9a-aa2c-0e506ffc5bb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>194</td>\n",
       "      <td>30</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>125</td>\n",
       "      <td>67</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>110</td>\n",
       "      <td>48</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     语文  数学   英语\n",
       "张三  194  30   47\n",
       "李四  125  67   34\n",
       "王五  110  48  100"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1+s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "7fff5003-dfc5-4930-b86b-949dd67a68ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>194</td>\n",
       "      <td>30</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>125</td>\n",
       "      <td>67</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>110</td>\n",
       "      <td>48</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     语文  数学   英语\n",
       "张三  194  30   47\n",
       "李四  125  67   34\n",
       "王五  110  48  100"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.add(s)\n",
    "df1.add(s,axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
